<?php
// +--------------------------------------------------------------------------+
// |							Kshop
// | 							Module for Xoops
// |            				www.kaotik.biz
// |            				kaotik1@gmail.com
// +--------------------------------------------------------------------------+


/**
* Class XoopsTabs is a simple class that lets you build tab menus
* @author	fatman	email: noah >4T< kerkness >D0T< ca
*/

class XoopsTabs
{

	/**
 * @var array   $tabs   an array of tabs
 */
	var	$tabs=array();

	/**
 * @var array   $tabs   an array of sublinks to display under an active tab
 */
	var	$subs=array();

	/**
 * @var string  $current_tab    the name of the current tab
 */
	var $current_tab;

	/**
 * @var string  $current_sub    the name of the current sub link
 */
	var $current_sub;

	/**
 * @var string  $_style    This is a name used in your style sheet
 */
	var $_style;

	/**
 * @var bool  $_return_style	set to true if build in style sheet is to be used    
 */
	var $_return_style = true;

	/**
 * Provides a way to add some text to far right side of tabs
 */
	var $_righttxt = "Kshop";

	/**
* XoopsTabs class constructor 
*/
	function XoopsTabs( $style = 'xtabs' )
	{
		$this->_style = $style;
		if ( $this->_style != 'xtabs' ) {
			$this->_return_style = false;
		}
	}

	/**
 * Returns array of tabs
 * @param   void
 */
	function getTabs()
	{
		return $this->tabs;
	}

	/**
 * Returns array of sub tabs
 * @param   void
 */
	function getSubs()
	{
		return $this->subs;
	}

	/**
 * Returns a multidimensional array of tabs with active tab info and current sub links
 * @param   void
 */
	function getSet()
	{
		return $this->fetchTabSet();
	}

	/**
  * Print the tabs to the browser
  */
	function display()
	{
		echo '<link rel="stylesheet" type="text/css" href="tabs.css" />';
		print $this->render();
	}

	/**
  * Assigns the html for all tabs to a single smarty tag.
  */
	function assign()
	{
		global $xoopsTpl;
		$xoopsTpl->assign( $this->_style, $this->render() );
	}

	/**
 * Method for setting the current tab or sub tab
 * @param	string	$name	name of the current link
 * @param	string	$set	either 'tabs' or 'subs' 
 */
	function setCurrent( $name, $set='tabs' )
	{
		if ( $set == 'tabs' ){
			$this->current_tab = $name;
		}
		if ( $set == 'subs' ){
			$this->current_sub = $name;
		}
	}

	/**
 * Method to add a single tab
 * @param	string	$name	a unique name for your link
 * @param	string	$link	the url for your link 
 * @param	string	$label	the text to display for link
 * @param	string	$weight	the display order ****   <--  doesn't do anything yet	
 */
	function addTab( $name, $link, $label, $weight=10 )
	{
		$this->addSet( 'tabs', $name, $link, $label, $weight );
	}

	/**
 * Method to add multiple tabs from an array of data
 * @param	array	$tabs 
 */
	function addTabArray( $tabs )
	{
		foreach ( $tabs as $name=>$tab )
		{
			$this->addSet( 'tabs', $name, $tab['link'], $tab['label'], $tab['weight'] );
		}
	}

	/**
 * Method to add a single sub link for display below an active tab
 * @param	string	$name	a unique name for your link
 * @param	string	$link	the url for your link 
 * @param	string	$label	the text to display for link
 * @param	string	$weight	the display order ****   <--  doesn't do anything yet	
 * @param	string	$parent	the name of the tab which this sublink should display under
 */
	function addSub( $name, $link, $label, $weight, $parent )
	{
		$this->addSet( 'subs', $name, $link, $label, $weight, $parent );
	}

	/**
 * Method to add multiple sub links from an array of data
 * @param	array	$subs 
 */
	function addSubArray( $subs )
	{
		foreach ( $subs as $name=>$sub )
		{
			$this->addSet( 'subs', $name, $tab['link'], $tab['label'], $tab['weight'] );
		}
	}

	/**
 * Method is used by the addTab and addSub methods and should not be called directly
 */
	function addSet( $set, $name, $link, $label, $weight, $parent=null )
	{
		if ( $set == 'tabs' )
		{
			$this->tabs[$name]['link'] = $link;
			$this->tabs[$name]['label'] = $label;
			$this->tabs[$name]['weight'] = $weight;
			$this->tabs[$name]['name'] = $name;
		} elseif ( $set == 'subs' ){
			$this->subs[$parent][$name]['link'] = $link;
			$this->subs[$parent][$name]['label'] = $label;
			$this->subs[$parent][$name]['weight'] = $weight;
			$this->subs[$parent][$name]['name'] = $name;
		}
	}

	/**
 * Method is used to clear all assigned sub links
 * @param   void
 */
	function clearSubs()
	{
		$this->subs = null;
	}

	/**
 * Method is used to build a complete set of data which can then be easily used
 * to display tabs in a webpage. This method should not be called directly and 
 * can be accessed via the getSet() method.
 * @return  array   full tab data and sub links for active tab
 * 
 */
	function fetchTabSet()
	{
		$set['tabs'] = $this->tabs;
		$subs = $this->subs;
		foreach ( $subs as $k=>$v )
		{
			if ( $k == $this->current_tab )
			{
				$set['subs'] = $v;
			}
		}

		if ( isset($this->current_tab) ){
			$set['tabs'][$this->current_tab]['current'] = 1;
		}
		if ( isset($this->current_sub) ){
			$set['subs'][$this->current_sub]['current'] = 1;
		}

		$set['tabcount'] = count($set['tabs']);
		if ( isset($set['subs'])) {
			$set['subcount'] = count($set['subs']);
		}

		return $set;
	}

	/**
  * Return the html which makes up the tabs.
  */
	function render()
	{
		$html = '';
		//if ( $this->_return_style ) $html .= $this->getStyle();
		$tabs = $this->getSet();

		$html .= "<div id='".$this->_style."'><del class='tabcontainer'>
		  <ul>";
		foreach ( $tabs['tabs'] as $k=>$tab )
		{
			$html .= "<li";
			if ( (isset($tab['current'])) &&  ($tab['current'] == 1 )) $html .= " id='current'";
			$html .= "><a href='".$tab['link']."'>".$tab['label']."</a></li>";
		}
		$html .= "<li id='rightside'>".$this->_righttxt."</li>
	 	  </ul>
		</del><del class='tabcontainer'>";
		$n = 0;
		$html .= "</del></div>";

		$html .= "<div id='sub".$this->_style."'>";
		if (isset($tabs['subs'])){
			foreach ( $tabs['subs'] as $k=>$sub )
			{
				if ( $n > 0 ) $html .= "| &nbsp;";
				$html .= "<a href='".$sub['link']."'>".$sub['label']."</a> &nbsp;";
				$n++;
			}
		}
		$html .= "</div>";

		return $html;
	}

} // END CLASS


?>